package com.example.newheze.vo;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.newheze.Adapter.RemoteRecycleAdapter;
import com.example.newheze.Control.RemoteControl;
import com.example.newheze.R;
import com.example.newheze.Util.HttpUtils;
import com.example.newheze.Util.Sendcommand;
import com.github.florent37.materialviewpager.header.MaterialViewPagerHeaderDecorator;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class RemoteRecycleFragment extends android.app.Fragment {
    public static RemoteRecycleFragment newInstance() {
        return new RemoteRecycleFragment();
    }

    final List<RemoteControl> items = new ArrayList<>();
    private boolean lightflag;
    private boolean waterflag;

    public RemoteRecycleFragment() {
    }

    RecyclerView mRecyclerView;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.recyclerview_remote, container, false);
    }

    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRecyclerView = view.findViewById(R.id.recyclerview_remote);
        new MyAsyncTask().execute("http://202.194.131.174:8080/newdongping/Wenshi_dongping_zt_servlet?plotid=009&num=1");

        items.add(new RemoteControl(R.drawable.remotecontrol, "信息采集", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Sendcommand("BB00dongping006EF")).start();
                new Thread(new Sendcommand("BB00dongping007EF")).start();
                new Thread(new Sendcommand("BB00dongping008EF")).start();
                new Thread(new Sendcommand("BB00dongping009EF")).start();
                Toast.makeText(getActivity(), "信息采集成功", Toast.LENGTH_SHORT).show();
            }
        }));

        items.add(new RemoteControl(R.drawable.sup_lighting, "补充光照", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
                alertDialog.setIcon(R.drawable.sup_lighting)
                        .setTitle("补充光照")
                        .setMessage("当前补光灯状态: " + (lightflag? "打开" : "关闭"))
                        .setPositiveButton("打开", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                new Thread(new Sendcommand("BB00dongping009FD")).start();
                                new MyAsyncTask().execute("http://202.194.131.174:8080/newdongping/Wenshi_dongping_zt_servlet?plotid=009&num=1");
                                Toast.makeText(getActivity(), "打开补光灯", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNegativeButton("关闭", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                new Thread(new Sendcommand("BB00dongping009FE")).start();
                                new MyAsyncTask().execute("http://202.194.131.174:8080/newdongping/Wenshi_dongping_zt_servlet?plotid=009&num=1");
                                Toast.makeText(getActivity(), "关闭补光灯", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .show();
            }
        }));

        items.add(new RemoteControl(R.drawable.fertilization, "水肥灌溉", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
                alertDialog.setIcon(R.drawable.fertilization)
                        .setTitle("水肥灌溉")
                        .setMessage("当前水泵状态: " + (waterflag ? "打开" : "关闭"))
                        .setPositiveButton("打开", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                new Thread(new Sendcommand("BB00dongping009FB")).start();
                                new MyAsyncTask().execute("http://202.194.131.174:8080/newdongping/Wenshi_dongping_zt_servlet?plotid=009&num=1");
                                Toast.makeText(getActivity(), "打开水泵", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNegativeButton("关闭", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                new Thread(new Sendcommand("BB00dongping009FC")).start();
                                new MyAsyncTask().execute("http://202.194.131.174:8080/newdongping/Wenshi_dongping_zt_servlet?plotid=009&num=1");
                                Toast.makeText(getActivity(), "关闭水泵", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .show();
            }
        }));

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.addItemDecoration(new MaterialViewPagerHeaderDecorator());
        mRecyclerView.setAdapter(new RemoteRecycleAdapter(this.getActivity(), items));

    }

    public class MyAsyncTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... strings) {
            return HttpUtils.sendPostMethod(strings[0],"utf-8");
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            try {
                JSONArray jsonArray = new JSONArray(s);
                JSONObject jsonObject = jsonArray.getJSONObject(0);
                Info_zt info_zt = new Info_zt(
                        null,
                        jsonObject.getString("datetime"),
                        jsonObject.getString("water_open"),
                        jsonObject.getString("water_close"),
                        null,
                        jsonObject.getString("SF_up"),
                        jsonObject.getString("water"),
                        jsonObject.getString("sun"),
                        jsonObject.getString("plotid"),
                        null,
                        null,
                        null
                );
               /* Log.d("sunleo",info_zt.getSun());
                Log.d("waterleo",info_zt.getWater());*/

                switch (info_zt.getSun()){
                    case "0.00":
                        lightflag =false;
                        break;
                    case "1.00":
                        lightflag =true;
                        break;
                }

                switch (info_zt.getWater()){
                    case "0.00":
                        waterflag=false;
                        break;
                    case "1.00":
                        waterflag=true;
                        break;
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    }
}
